layui.define(["jquery"], function (exports) {
	var jQuery = layui.jquery,
	$ = jQuery,
	vsscrollbar = function () {
		this.config = {
			elem:'',
			vsbar:'.layui-vsscrollbar',
			selected:4,
			selectdom:'.layui-vsscrollbar-selected',
			item:'.layui-vsscrollbar-item',
			active:'active',
			size:28,
			minsize:16,
			mino : 0.1,
			height:46,
			maxlen : 10,
		}
	};
	vsscrollbar.prototype.set = function (option) {
		var t = this;
		$.extend(true, t.config, option);
		return t.config;
	},
	vsscrollbar.prototype.render = function (options,callback) {
		var obj = new vsscrollbar();
		var option = obj.set(options);
		var len = $(option.elem).find(option.item).children().length;
		var middle = len%2>0 ? parseInt(len / 2) : parseInt(len / 2) - 1;
		var maxmiddle = option.maxlen%2>0 ? parseInt(option.maxlen / 2) : parseInt(option.maxlen / 2) - 1;
		var speed = parseInt(10/(middle+1)/10*100)/100;
		option.len = len;
		option.middle = middle;
		option.maxmiddle = maxmiddle;
		option.speed = speed;
		option.top = parseInt($(option.elem).css("padding-top"));
		option.nums = vray(option);
		move(option,callback);
	};
	var vray = function(option){
		var nums=[];
		$(option.elem).find(option.item).height(option.height*option.maxlen);
		$(option.elem).height(option.height*option.maxlen);
		$(option.elem).find(option.selectdom).css("top",option.height*option.maxmiddle+option.top);
		nums.push(option.maxmiddle);
		$(option.elem).find(option.item).children().eq(option.selected).attr("num",option.maxmiddle).addClass(option.active).children().css("font-size",option.size);
		var pl = $(option.elem).find(option.item).children().eq(option.selected).prevAll().length;
		var n = option.maxmiddle-1;
		for(var i=option.selected-1;i>option.selected-1-option.middle;i--){
			if(i<0){
				$(option.elem).find(option.item).children().eq(option.len+i).attr("num",n);
			}else{
				$(option.elem).find(option.item).children().eq(i).attr("num",n);
			}
			nums.push(n);
			n--;
		}
		var n = option.maxmiddle+1;
		for(var i=option.selected+1;i<option.selected+1+option.len-(option.middle+1);i++){
			if(i>option.len-1){
				$(option.elem).find(option.item).children().eq(i-option.len+1).attr("num",n);
			}else{
				$(option.elem).find(option.item).children().eq(i).attr("num",n);
			}
			nums.push(n);
			n++;
		}
		$(option.elem).find(option.item).children().each(function(i,ele){
			var n = parseInt($(this).attr("num"));
			$(this).css({position:"absolute",top:n*option.height});
			if(n < option.maxmiddle){
				var o = 1-(option.maxmiddle-n)*option.speed;
				o = o.toFixed(2);
				console.log(o,n);
				var size = option.size-(option.maxmiddle-n)*2;
				if(size<option.minsize){
					size = option.minsize
				}
				$(this).children().css({opacity:o,fontSize:size});
			}
			if(n > option.maxmiddle){
				var o = 1-option.speed*(n-option.maxmiddle);
				o = o.toFixed(2);
				if(o < option.mino){
					o = option.mino;
				}
				console.log(o,n);
				var size = option.size-(n-option.maxmiddle)*2;
				if(size<option.minsize){
					size = option.minsize
				}
				$(this).children().css({opacity:o,fontSize:size})
			}
		})
		return nums;
	}
	var move = function(option,callback){
		var max = Math.max.apply(null,option.nums);
		var min = Math.min.apply(null,option.nums);
		$(option.elem).find(option.item).children().each(function(i){
			var item = i;
			$(this).on("click",function(){
				var d = option.maxmiddle - parseInt($(this).attr("num"));
				$(this).children().animate({fontSize:option.size,opacity:1});
				$(this).parent().children().each(function(i,ele){
					var n = parseInt($(ele).attr('num'));
					var num = n + d;
					if(num<min){
						num += option.len;
					}
					if(num>max){
						num -= option.len;
					}
					$(ele).attr('num',num);
					$(ele).animate({top:num*option.height});
					if(num<option.maxmiddle){
						var o = 1-(option.maxmiddle-num)*option.speed;
						if(o<option.mino){
							o = option.mino
						}
						var size = option.size-(option.maxmiddle-num)*2;
						if(size<option.minsize){
							size = option.minsize
						}
						$(ele).children().css({opacity:o,fontSize:size})
					}
					if(num>option.maxmiddle){
						var o = 1-(num-option.maxmiddle)*option.speed;
						if(o<option.mino){
							o = option.mino
						}
						var size = option.size-(num-option.maxmiddle)*2;
						if(size<option.minsize){
							size = option.minsize
						}
						$(ele).children().css({opacity:o,fontSize:size})
					}
				})
				$(this).addClass("active");
				$(this).siblings().removeClass("active");
				var obj = {ele:this,value:$(this).text()}
				if(callback){
					callback(obj);
				}
			})
		})
	}
	var Vsscrollbar = new vsscrollbar();
	exports('vsscrollbar', Vsscrollbar);
})